﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BoAddonBiz_FreightForwarder.BusinessBase;
using SAPbouiCOM;
using SAPBoAddon;

namespace BoAddonBiz_FreightForwarder.Sea
{
    [ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_SE_Delegate)]
    class B1Form_Delegate_Sea : B1Form_Delegate_Base
    {
        #region override
        protected override string AVA_ODLG { get { return "@AVA_SE_ODLG"; } }
        protected override string AVA_DLG1 { get { return "@AVA_SE_DLG1"; } }
        protected override string AVA_DLG2 { get { return "@AVA_SE_DLG2"; } }
        protected override string AVA_DLG3 { get { return "@AVA_SE_DLG3"; } }
        protected override string AVA_DLG4 { get { return "@AVA_SE_DLG4"; } }
        protected override string AVA_OQOT { get { return "@AVA_SE_OQOT"; } }
        protected override string AVA_QOT3 { get { return "@AVA_SE_QOT3"; } }
        protected override string AVA_OSOD { get { return "@AVA_SE_OSOD"; } }
        protected override string AVA_SOD1 { get { return "@AVA_SE_SOD1"; } }
        protected override string AVA_SOD2 { get { return "@AVA_SE_SOD2"; } }
        protected override string AVA_SOD3 { get { return "@AVA_SE_SOD3"; } }
        protected override string BusinessType { get { return "SE"; } }
        protected override string TargetObject { get { return "AVA_SE_BusinessOpera"; } }
        protected override string SourceSalesOrderObject { get { return "AVA_SE_SalesOrder"; } }
        protected override string SourceQuoteObject { get { return "AVA_SE_Quote"; } }
        private string AVA_DLG5 { get { return "@AVA_SE_DLG5"; } }
        protected override string[] SumMapping
        {
            get { return new string[] { "U_Quantity:U_Quantity", "U_GrossWeight:U_GrossWeight", "U_BillingWeight:U_BillingWeight", "U_Volume:U_Volume", "U_CargoValue:U_InvoiceValue" }; }
        }
        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_SE_Delegate;
            FormSetting.FormFileName = B1FormTypes.ado_AVA_SE_Delegate.ToString() + "Form.srf";
            base.FormInfoSetting(FormSetting);
            #region Matrix
            //配置添加子表的行
            FormSetting.MatrixSetting.MtxUID = "mtxService";
            FormSetting.MatrixSetting.NotAllowNoLineSave = false;
            FormSetting.MatrixSetting.AutoAddNewLine = true;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_2";//箱量
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = false;
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = false;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = AVA_DLG5;
            FormSetting.MatrixSetting.Add();
            #endregion

        }
        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            base.FormEditModeChange(frmEditMode);

            switch (frmEditMode)
            {
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
                    //SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtxService", "col_f", AVA_DLG5, -1, "Lineid", "VisOrder", true);
                    break;
            }
        }
        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            base.InitializeForm(InitPar);
            Utils.InitialMatrixComboBox(this.SBOCompany, this.CurrentForm.Items.Item("mtxService").Specific, "col_1", "Code", "Name", "@AVA_FF_OBOX", "Code", null, null, true, true);
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("cargo_46"), "Code", "Name", "@AVA_FF_OPRI", "Code", null, "U_Type = N'Number' OR U_Type = N'Pack'", true, false, true);// 包装形式 取值 计量单位里的 by WangPeng 2012年8月29日 19:34:58
            if (this.CurrentForm.Mode == BoFormMode.fm_ADD_MODE && InitPar.FatherFormUID.Contains("Quote"))
            {
                Form fatherForm = SBOApp.Forms.Item(InitPar.FatherFormUID);
                DBDataSource dlg1 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1);
                DBDataSource qot3 = fatherForm.DataSources.DBDataSources.Item("@AVA_SE_QOT3");
                for (int i = 0; i < dlg1.Size; i++)
                {
                    dlg1.SetValue("U_OrderNumber", i, qot3.GetValue("U_OrderNo", i));
                    dlg1.SetValue("U_ConNumber", i, qot3.GetValue("U_ContractNo", i));
                }
                (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).LoadFromDataSource();
            }
        }

        #endregion
        #region Constractor
        public B1Form_Delegate_Sea()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Delegate_Sea_frm_ItemEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Delegate_Sea_frm_ChooseFormListEvent);
            this.frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_Delegate_Sea_frm_FormDataEvent);
            this.frm_MenuEvent += new IB1FormEvents.B1MenuEventEventHandler(B1Form_Delegate_Sea_frm_MenuEvent);
        }
        #endregion
        #region Events
        void B1Form_Delegate_Sea_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.MenuUID == "1292")//添加行
                {
                    if (this.CurrentForm.Settings.MatrixUID == "mtxService")
                    {
                        Matrix mtx = this.CurrentForm.Items.Item(this.CurrentForm.Settings.MatrixUID).Specific;
                        SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, this.CurrentForm.Settings.MatrixUID, "col_f", mtx.Columns.Item("col_f").DataBind.TableName, -1, "LineId", "VisOrder", true);
                    }
                }
                else if (pVal.MenuUID == "1293")//删除行
                {
                    if (this.CurrentForm.Settings.MatrixUID == "mtxService")
                    {
                        Matrix mtx = this.CurrentForm.Items.Item(this.CurrentForm.Settings.MatrixUID).Specific;
                        if (mtx.GetCellFocus() != null)
                        {
                            SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, mtx.Columns.Item("col_f").DataBind.TableName, "LineId", this.CurrentForm.Settings.MatrixUID, "col_f", mtx.GetCellFocus().rowIndex, true);
                        }
                    }
                }
            }
            else
            {

            }
        }

        void B1Form_Delegate_Sea_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_ADD || pVal.EventType == BoEventTypes.et_FORM_DATA_UPDATE)
                {
                    (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).LoadFromDataSource();
                }
            }
            else
            {

            }
        }

        void B1Form_Delegate_Sea_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            SAPbouiCOM.DBDataSource dbds = default(SAPbouiCOM.DBDataSource);
            SAPbouiCOM.Conditions cons;
            SAPbouiCOM.Condition con;
            SAPbouiCOM.ChooseFromList cfl;
            SAPbobsCOM.Recordset oRst = (SAPbobsCOM.Recordset)SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            if (pVal.Before_Action)
            {
                cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_SPAgencyInfoCardCode"://起运港代理
                    case "CFL_EPAgencyNameCardCode"://目的港代理
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup11";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup12";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_TPAgencyNameCardCode"://中转港代理
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_CarrierNameCardCode"://承运人
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup4";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_BookingAgentCardCode"://订舱代理
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup4";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup5";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                }
            }
            else
            {
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_BookingAgentCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_ODLG);
                        dbds.SetValue("U_BookingAgentCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString().Trim());
                        dbds.SetValue("U_BookingAgentName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        break;
                }
            }
        }

        void B1Form_Delegate_Sea_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                if (pVal.ItemUID == "1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    if (this.CurrentForm.Mode != BoFormMode.fm_OK_MODE)
                    {
                        (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).LoadFromDataSource();
                    }
                }
            }
            else
            {
                #region 事件后
                #region Switch Panel
                if (pVal.ItemUID == "tabService" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    switch (this.CurrentForm.DataSources.DBDataSources.Item(AVA_ODLG).GetValue("U_ContainerType", 0).Trim())
                    {
                        case "FCL":
                        case "1":
                            CurrentForm.PaneLevel = 21;
                            break;
                        case "LCL":
                        case "2":
                            CurrentForm.PaneLevel = 22;
                            break;
                        case "BREAKBULK":
                        case "3":
                            CurrentForm.PaneLevel = 23;
                            break;
                        default:
                            CurrentForm.PaneLevel = 21;
                            break;
                    }
                }
                if (pVal.ItemUID == "FCL" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentForm.PaneLevel = 21;
                }
                if (pVal.ItemUID == "LCL" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentForm.PaneLevel = 22;
                }
                if (pVal.ItemUID == "BULK" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentForm.PaneLevel = 23;
                }
                #endregion

                if (pVal.ItemUID == "Chk_In" && (pVal.EventType == BoEventTypes.et_ITEM_PRESSED || pVal.ItemChanged))
                {
                    this.CurrentForm.Items.Item("T_Company").Enabled = false;
                    if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, pVal.ItemUID).ToString().Trim() == "Y")
                    {
                        this.CurrentForm.Items.Item("T_Company").Enabled = true;
                    }
                }
                #endregion

                #region 海运特有，改变单件重量的时候同时计算计费重
                if (pVal.EventType == BoEventTypes.et_COMBO_SELECT && new string[] { "cargo_30", "cargo_72" }.Contains<string>(pVal.ItemUID))
                {
                    Utils.CalculateVolumeAndQuantity(SBOApp, SBOCompany, CurrentForm, AVA_DLG1, CargoCurrentOffset);
                }
                if (new string[] { "cargo_56", "cargo_62", "cargo_68", "cargo_40" }.Contains<string>(pVal.ItemUID) && pVal.ItemChanged)
                {
                    string value = string.Empty;
                    switch (pVal.ItemUID)
                    {
                        case "cargo_56":
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_TraAmount", CargoCurrentOffset).Trim();
                            break;
                        case "cargo_62":
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_ConAmount", CargoCurrentOffset).Trim();
                            break;
                        case "cargo_68":
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_InvoiceValue", CargoCurrentOffset).Trim();
                            break;
                        case "cargo_40"://货值
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_CargoValue", CargoCurrentOffset).Trim();
                            break;
                    }
                    double origin = 0.0d;
                    double.TryParse(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_TraAmount", CargoCurrentOffset), out origin);
                    if (origin == 0.0d)
                    {
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_TraAmount", CargoCurrentOffset, value);
                    }
                    double.TryParse(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_ConAmount", CargoCurrentOffset), out origin);
                    if (origin == 0.0d)
                    {
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_ConAmount", CargoCurrentOffset, value);
                    }
                    double.TryParse(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_InvoiceValue", CargoCurrentOffset), out origin);
                    if (origin == 0.0d)
                    {
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_InvoiceValue", CargoCurrentOffset, value);
                    }
                    double.TryParse(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_CargoValue", CargoCurrentOffset), out origin);
                    if (origin == 0.0d)
                    {
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_CargoValue", CargoCurrentOffset, value);
                    }
                    Utils.MaterialSum(this.CurrentForm, AVA_ODLG, AVA_DLG1, SumMapping);
                }
                if (new string[] { "cargo_58", "cargo_64", "cargo_70", "cargo_42" }.Contains<string>(pVal.ItemUID) && pVal.EventType == BoEventTypes.et_COMBO_SELECT)
                {
                    string value = string.Empty;
                    switch (pVal.ItemUID)
                    {
                        case "cargo_58"://订单币种
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_TraCurrency", CargoCurrentOffset).Trim();
                            break;
                        case "cargo_64"://合同币种
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_ConCurrency", CargoCurrentOffset).Trim();
                            break;
                        case "cargo_70"://发票币种
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_InvoiceCurrency", CargoCurrentOffset).Trim();
                            break;
                        case "cargo_42"://币种
                            value = this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_Currency", CargoCurrentOffset).Trim();
                            break;
                    }
                    if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_TraCurrency", CargoCurrentOffset).Trim() == "")
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_TraCurrency", CargoCurrentOffset, value);
                    if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_ConCurrency", CargoCurrentOffset).Trim() == "")
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_ConCurrency", CargoCurrentOffset, value);
                    if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_InvoiceCurrency", CargoCurrentOffset).Trim() == "")
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_InvoiceCurrency", CargoCurrentOffset, value);
                    if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_Currency", CargoCurrentOffset).Trim() == "")
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).SetValue("U_Currency", CargoCurrentOffset, value);
                }
                #endregion

                #region 根据计费吨标准自动计算计费重量
                if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_ODLG).GetValue("U_Status", 0).Trim() == "P")
                {
                    if (pVal.ItemUID == "LCL" && pVal.EventType == BoEventTypes.et_CLICK)
                    {
                        ComboBox cb = this.CurrentForm.Items.Item("T_Standard").Specific as ComboBox;
                        string standard = "1000KGS";
                        if (!String.IsNullOrEmpty(cb.Value))
                        {
                            standard = cb.Value;
                        }
                        Utils.CalcBillingWeight(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1), this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG5), standard);
                    }
                    if (pVal.ItemUID == "T_Standard" && pVal.EventType == BoEventTypes.et_COMBO_SELECT)
                    {
                        ComboBox cb = this.CurrentForm.Items.Item("T_Standard").Specific as ComboBox;
                        string standard = "1000KGS";
                        if (!String.IsNullOrEmpty(cb.Value))
                        {
                            standard = cb.Value;
                        }
                        Utils.CalcBillingWeight(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1), this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG5), standard);
                    }
                }
                #endregion

                #region 修改计费吨重时同步修改货物表的计费重量
                if (pVal.ItemUID == "T_Weight" && pVal.ItemChanged)
                {
                    Utils.SetCargoGrossWeight(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1), SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, pVal.ItemUID).ToString());
                }
                #endregion
                if (pVal.ItemUID == "mtxService" && pVal.ColUID == "col_2" && pVal.ItemChanged)
                {
                    this.CurrentForm.Freeze(true);
                    (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).FlushToDataSource();
                    //不知道为什么MatrixSetting不管用了，所以手动加了下面的代码
                    //double quantity = 0.0d;
                    //double.TryParse(this.CurrentForm.DataSources.DBDataSources.Item(AVA_DLG1).GetValue("U_Quantity", pVal.Row - 1), out quantity);
                    //if (quantity > 0.0d && pVal.Row == (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).VisualRowCount)
                    //{
                    //    SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtxService", "col_f", AVA_DLG1, -1, "Lineid", "VisOrder", true);
                    //}
                    this.CurrentForm.Freeze(false);
                }
            }
        }
        #endregion

    }
}
